// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import Common from "../Common";
import {isEmpty} from "../common/Helpers";

const {ccclass, property} = cc._decorator;

@ccclass
export default class RegisterDialog extends Common {

    @property({
        type: cc.Node,
        displayName: '注册按钮'
    })
    registerBtn: cc.Node;

    protected formData: any = {
        username: '',
        code: '',
        password: '',
        rePassword: '',
        inviteCode: '',
    }

    start() {
        this.registerBtn.on(cc.Node.EventType.TOUCH_START, this.onRegister, this)
    }


    async onRegister() {
        this.onBtnClick()
        const formData = this.formData;
        if (isEmpty(formData.username)) {
            return this.showToast('请输入用户名')
        }
        // if (isEmpty(formData.code)) {
        //     return this.showToast('验证码必须填写')
        // }
        if (isEmpty(formData.password)) {
            return this.showToast('请输入密码')
        }
        if (formData.password !== formData.rePassword) {
            return this.showToast('确认密码不一致')
        }
        if (isEmpty(formData.inviteCode)) {
            return this.showToast('推荐人必须填写')
        }

        try {
            const {message} = await this.http.post('auth/register', formData)
            this.showToast(message);
            this.closeToast(1);
            this.node.active = false;
        } catch ({message}) {
            this.showToast(message);
        }

    }

}
